﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using UITLibraryManagement.ModelValidators;
using UITLibraryManagement.View;
using UITLibraryManagement.ViewModel;
using System.Windows.Forms;
using System.IO;

namespace UITLibraryManagement
{
    /// <summary>
    /// Interaction logic for demosAPI.xaml
    /// </summary>
    public partial class demosAPI : Window, IView
    {
        public demosAPI()
        {
            InitializeComponent();

            IDocGiaRepository docgiaRepository = App.GetInstance<IDocGiaRepository>();
            List<DOCGIA> listResult = docgiaRepository.GetListDocGia();
            List<DocGiaValidator> listDocgia = (from q in listResult
                                                select new DocGiaValidator(q)).ToList<DocGiaValidator>();

            

            myDataGrid.ItemsSource = listDocgia;


        }

        

        private void btnUpLoad_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog fileDialog = new System.Windows.Forms.OpenFileDialog();
            fileDialog.Title = "Select photo to upoad";
            fileDialog.InitialDirectory = "";
            fileDialog.Filter = "Image Files (*.gif,*.jpg,*.jpeg,*.bmp,*.png)|*.gif;*.jpg;*.jpeg;*.bmp;*.png";
            fileDialog.FilterIndex = 1;

            if (fileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                tbFileName.Text = fileDialog.FileName;
                lblUploadNotification.Content = GetFileNameNoExt(tbFileName.Text.Trim());
                BitmapImage bmp = new BitmapImage(new Uri(tbFileName.Text.Trim()));
                imgAvatar.Source = bmp;
            }
            else
            {
                lblUploadNotification.Content = "You didn't select any image file....";
            }
        }

        public string GetFileNameNoExt(string filePathFileName)
        {
            return System.IO.Path.GetFileNameWithoutExtension(filePathFileName);
        }

        private void btnSavePhoto_Click(object sender, RoutedEventArgs e)
        {
            FileStream stream = new FileStream(tbFileName.Text, FileMode.Open, FileAccess.Read);
            StreamReader reader = new StreamReader(stream);
            Byte[] imgData = new Byte[stream.Length - 1];
            stream.Read(imgData, 0, (int)stream.Length - 1);

            IDocGiaRepository docgiaRepository = App.GetInstance<IDocGiaRepository>();
            DOCGIA docgia = docgiaRepository.GetAll().First();
            docgia.Avatar = imgData;
            docgiaRepository.SaveChanges();
            lblUploadNotification.Content = "Save successfully";
            /*
             * http://code.msdn.microsoft.com/windowsdesktop/WPF-Upload-Image-Using-b8d1daed#content
             */
        }



        public ViewModel.IViewModel ViewModel
        {
            get
            {
                return DataContext as IViewModel;
            }
            set
            {
                DataContext = value;
            }
        }
    }
}
